home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 4287 / 4287.xpi / chrome / splitbrowser.jar / content / splitbrowser / fullScreenCanvas.xul < prev    next >
Extensible Markup Language  |  2009-11-05  |  8KB  |  284 lines

  1. <?xml version="1.0"?>
  2. <!--
  3.  Full Screen Canvas
  4.  
  5.  Usage:
  6.    window.fullScreenCanvas.show();
  7.    ... // do something
  8.    window.fullScreenCanvas.hide();
  9.    var zoom = window.fullScreenCanvas.getZoomForFrame(window.content);
  10.  
  11.  lisence: The MIT License, Copyright (c) 2009 SHIMODA "Piro" Hiroshi
  12.    http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/license.txt
  13.  original:
  14.    http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/fullScreenCanvas.xul
  15. -->
  16. <overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  17. <script type="application/x-javascript"><![CDATA[
  18.  
  19. window.addEventListener('DOMContentLoaded', function() {
  20.     window.removeEventListener('DOMContentLoaded', arguments.callee, true);
  21.  
  22.     const currentRevision = 8;
  23.     var root = document.documentElement;
  24.  
  25.     var loadedRevision = root.getAttribute('fullScreenCanvas');
  26.     if (loadedRevision) {
  27.         loadedRevision = Number(loadedRevision);
  28.         if (loadedRevision >= currentRevision) {
  29.             return;
  30.         }
  31.         else if (loadedRevision < currentRevision) {
  32.             root.setAttribute('fullScreenCanvas', currentRevision);
  33.             window.fullScreenCanvas.destroy();
  34.         }
  35.     }
  36.  
  37.     window.fullScreenCanvas = {
  38.         show : function(aTargetElement) 
  39.         {
  40.             if (this.shown) return;
  41.             this.shown = true;
  42.  
  43.             var color = '-moz-field';
  44.             var canvas = this.canvas;
  45.             if (!canvas) return;
  46.  
  47.             var rootBox = document.documentElement.boxObject;
  48.             var canvasW = window.innerWidth;
  49.             var canvasH = window.innerHeight;
  50.  
  51.             var x, y, w, h;
  52.             if (aTargetElement && aTargetElement.ownerDocument == document) {
  53.                 var box = aTargetElement.boxObject;
  54.                 y = box.screenY - rootBox.screenY;
  55.                 x = box.screenX - rootBox.screenX;
  56.                 w = box.width;
  57.                 h = box.height;
  58.             }
  59.             else {
  60.                 x = y = 0;
  61.                 w = canvasW;
  62.                 h = canvasH;
  63.             }
  64.  
  65.             canvas.style.left   = 0;
  66.             canvas.style.top    = 0;
  67.             canvas.style.width  = (canvas.width = canvasW)+'px';
  68.             canvas.style.height = (canvas.height = canvasH)+'px';
  69.             canvas.style.zIndex = 65000;
  70.             var frame = this.frame;
  71.             if (frame) {
  72.                 frame.style.width  = canvas.style.width;
  73.                 frame.style.height = canvas.style.height;
  74.             }
  75.             try {
  76.                 var ctx = canvas.getContext('2d');
  77.                 ctx.clearRect(0, 0, canvasW, canvasH);
  78.                 ctx.save();
  79.                 ctx.translate(x, y);
  80.                 ctx.drawWindow(window, x, y, w, h, color);
  81.                 ctx.restore();
  82.  
  83.                 var browsers = this.browsers;
  84.                 var self = this;
  85.                 browsers.forEach(function(aBrowser) {
  86.                 try {
  87.                     var b = aBrowser;
  88.                     if (b.localName == 'subbrowser') b = b.browser;
  89.                     var TST = b.treeStyleTab;
  90.                     var frame = b.contentWindow;
  91.                     var zoom = self.getZoomForFrame(frame);
  92.                     var x = (b.localName == 'tabbrowser' ? b.mCurrentBrowser : b ).boxObject.x;
  93.                     var y = (b.localName == 'tabbrowser' ? b.mCurrentBrowser : b ).boxObject.y;
  94.                     var w = frame.innerWidth;
  95.                     var h = frame.innerHeight;
  96.                     var dx = 0;
  97.                     var dy = 0;
  98.                     if (
  99.                         TST &&
  100.                         TST.autoHideEnabled &&
  101.                         ('autoHideShown' in TST ? 
  102.                             TST.autoHideShown :
  103.                             TST.tabbarShown
  104.                         )
  105.                         ) {
  106.                         var pos = b.getAttribute(TST.kTABBAR_POSITION);
  107.                         var xOffset = 'autoHideXOffset' in TST ? TST.autoHideXOffset : TST.tabbarWidth ;
  108.                         var yOffset = 'autoHideYOffset' in TST ? TST.autoHideYOffset : TST.tabbarHeight ;
  109.                         switch (pos)
  110.                         {
  111.                             case 'left':
  112.                                 dx = xOffset / zoom;
  113.                                 w -= xOffset / zoom;
  114.                                 break;
  115.                             case 'right':
  116.                                 x += xOffset / zoom;
  117.                                 w -= xOffset / zoom;
  118.                                 break;
  119.                             case 'top':
  120.                                 dy = yOffset / zoom;
  121.                                 h -= yOffset / zoom;
  122.                                 break;
  123.                             case 'bottom':
  124.                                 y += yOffset / zoom;
  125.                                 h -= yOffset / zoom;
  126.                                 break;
  127.                         }
  128.                     }
  129.                     ctx.save();
  130.                     ctx.translate(x, y);
  131.                     ctx.save();
  132.                     ctx.scale(zoom, zoom);
  133.                     ctx.drawWindow(frame, dx+frame.scrollX, dy+frame.scrollY, w, h, color);
  134.                     ctx.restore();
  135.                     ctx.restore();
  136.                 }
  137.                 catch(e) {
  138.                 }
  139.                 });
  140.  
  141.                 canvas.style.position = 'fixed';
  142.                 this.container.removeAttribute('collapsed');
  143.             }
  144.             catch(e) {
  145.                 this.container.setAttribute('collapsed', true);
  146.             }
  147.         },
  148.         shown : false,
  149.  
  150.         getZoomForFrame : function(aFrame)
  151.         {
  152.             const Prefs = Components
  153.                     .classes['@mozilla.org/preferences;1']
  154.                     .getService(Components.interfaces.nsIPrefBranch);
  155.             try {
  156.                 if (Prefs.getBoolPref('browser.zoom.full')) {
  157.                     var zoom = aFrame
  158.                         .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  159.                         .getInterface(Components.interfaces.nsIWebNavigation)
  160.                         .QueryInterface(Components.interfaces.nsIDocShell)
  161.                         .contentViewer
  162.                         .QueryInterface(Components.interfaces.nsIMarkupDocumentViewer)
  163.                         .fullZoom;
  164.                     return (zoom * 1000 % 1) ? zoom+0.025 : zoom ;
  165.                 }
  166.             }
  167.             catch(e) {
  168.             }
  169.             return 1;
  170.         },
  171.  
  172.         hide : function()
  173.         {
  174.             if (!this.shown) return;
  175.             var canvas = this.canvas;
  176.             canvas.style.position = 'static';
  177.             canvas.style.width = canvas.style.height = canvas.style.zIndex = 0;
  178.             this.container.setAttribute('collapsed', true);
  179.             this.shown = false;
  180.         },
  181.  
  182.         get browsers()
  183.         {
  184.             browsers = [].concat(Array.slice(document.getElementsByTagName('tabbrowser')))
  185.                         .concat(Array.slice(document.getElementsByTagName('browser')));
  186.             if ('SplitBrowser' in window) browsers = browsers.concat(SplitBrowser.browsers);
  187.             if (this.frame) {
  188.                 var index = browsers.indexOf(this.frame);
  189.                 browsers.splice(index, 1);
  190.             }
  191.             return browsers;
  192.         },
  193.  
  194.         get frame()
  195.         {
  196.             return document.getElementById('fullScreenCanvas-frame');
  197.         },
  198.  
  199.         get canvas()
  200.         {
  201.             if (!this.frame) return this._canvas;
  202.  
  203.             var doc = this.frame.contentDocument;
  204.             var canvas = doc.getElementById('fullScreenCanvas-canvas');
  205.             if (canvas) return canvas;
  206.  
  207.             canvas = doc.importNode(this._canvas.cloneNode(true), true);
  208.             var root = doc.documentElement;
  209.             root.setAttribute('style', 'overflow: none; margin: 0;padding: 0;');
  210.             while (root.hasChildNodes())
  211.             {
  212.                 root.removeChild(root.firstChild);
  213.             }
  214.             root.appendChild(canvas);
  215.             return canvas;
  216.         },
  217.         _canvas : null,
  218.  
  219.         get container()
  220.         {
  221.             return document.getElementById('fullScreenCanvas-container');
  222.         },
  223.  
  224.         init : function() {
  225.             var canvas = document.createElementNS('http://www.w3.org/1999/xhtml', 'canvas');
  226.             canvas.setAttribute('id', 'fullScreenCanvas-canvas');
  227.             canvas.setAttribute('width', '0');
  228.             canvas.setAttribute('height', '0');
  229.             canvas.setAttribute('style', 'width:0;height:0;');
  230.             this._canvas = canvas;
  231.             if (!this.isGecko19) {
  232.                 this.container.appendChild(canvas);
  233.                 return
  234.             }
  235.             /* We have to put canvas into a frame because Gecko 1.9
  236.                renders canvas under the main content area if it is
  237.                not in a frame.
  238.             */
  239.             var frame = document.createElement('browser');
  240.             frame.setAttribute('id', 'fullScreenCanvas-frame');
  241.             frame.setAttribute('disablehistory', 'true');
  242.             this.container.appendChild(frame);
  243.             frame.style.width = 0;
  244.             frame.style.height = 0;
  245.             frame.style.borderWidth = 0;
  246.             frame.style.margin = 0;
  247.             frame.style.padding = 0;
  248.         },
  249.  
  250.         get isGecko19()
  251.         {
  252.             const XULAppInfo = Components.classes['@mozilla.org/xre/app-info;1']
  253.                     .getService(Components.interfaces.nsIXULAppInfo);
  254.             var version = XULAppInfo.platformVersion.split('.');
  255.             return parseInt(version[0]) >= 2 || parseInt(version[1]) >= 9;
  256.         },
  257.  
  258.         destroy : function() {
  259.             var range = document.createRange();
  260.             range.selectNodeContents(this.container);
  261.             range.deleteContents();
  262.             range.detach();
  263.         }
  264.     };
  265.  
  266.     fullScreenCanvas.init();
  267. }, true);
  268.  
  269. ]]></script>
  270.  
  271. <window id="main-window">
  272.     <vbox id="fullScreenCanvas-container"
  273.         collapsed="true"
  274.         style="
  275.             position: fixed;
  276.             z-index: 60000;
  277.             top: 0;
  278.             left: 0;
  279.         "
  280.         onclick="fullScreenCanvas.hide();"/>
  281. </window>
  282.  
  283. </overlay>
  284.